package com.hanthink.system.service.impl.pc;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanthink.common.constant.BusinessConstants;
import com.hanthink.common.core.domain.entity.SysDept;
import com.hanthink.common.core.domain.entity.SysUser;
import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.DateUtils;
import com.hanthink.common.utils.SerialNumberUtil;
import com.hanthink.system.domain.SysSerialNumber;
import com.hanthink.system.domain.pc.EmEquip;
import com.hanthink.system.domain.pc.EmTask;
import com.hanthink.system.domain.pc.EmTaskItem;
import com.hanthink.system.mapper.EmEquipMapper;
import com.hanthink.system.mapper.EmTaskItemMapper;
import com.hanthink.system.mapper.EmTaskMapper;
import com.hanthink.system.mapper.SysDeptMapper;
import com.hanthink.system.mapper.SysSerialNumberMapper;
import com.hanthink.system.mapper.SysUserMapper;
import com.hanthink.system.service.pc.IEmTaskService;

/**
 * 任务管理Service业务层处理
 * 
 * @author hanthink
 * @date 2021-01-22
 */
@Service
public class EmTaskServiceImpl implements IEmTaskService {
	@Autowired
	private EmTaskMapper emTaskMapper;


	@Autowired
	private EmTaskItemMapper emTaskItemMapper;

	@Autowired
	private EmEquipMapper emEquipMapper;

	@Autowired
	private SysDeptMapper deptMapper;

	@Autowired
	private SysSerialNumberMapper sysSerialNumberMapper;
	
	@Autowired
	private SysUserMapper sysUserMapper;

	/**
	 * 查询任务管理
	 * 
	 * @param taskCode 任务管理ID
	 * @return 任务管理
	 */
	@Override
	public EmTask selectEmTaskById(String taskCode) {
		return emTaskMapper.selectEmTaskById(taskCode);
	}


	@Override
	public List<String> selectEmTaskListbydatetimeanddeptid(String deptid){
		return emTaskMapper.selectEmTaskListbydatetimeanddeptid(deptid);
	}
	/**
	 * 查询任务管理列表
	 * 
	 * @param emTask 任务管理
	 * @return 任务管理
	 */
	@Override
	public List<EmTask> selectEmTaskList(EmTask emTask) {
		return emTaskMapper.selectEmTaskList(emTask);
	}

	@Override
	public String selectEmTaskListPlus(String equipname) {
		return emTaskMapper.selectEmTaskListPlus(equipname);
	}

	@Override
	public List<EmTask> selectEmTaskListQA(String  datestring) {
		return emTaskMapper.selectEmTaskListQA(datestring);
	}

	/**
	 * 新增任务管理
	 * 
	 * @param emTask 任务管理
	 * @return 结果
	 */
	@Override
	public int insertEmTask(EmTask emTask) {

		if (emTask.getTaskCode() == null) {
			// 获取序列号
			SysSerialNumber serial = sysSerialNumberMapper.selectNextValue();
			String code = SerialNumberUtil.getSerialNumber(BusinessConstants.PLAN_TASK, serial.getNextValue(),
					serial.getToday());
			emTask.setTaskCode(code);
		}

		// 校验用户在系统在是否存在
		SysUser sysUser = new SysUser();
		sysUser.setNickName(emTask.getExecuter());
		List<SysUser> userList = sysUserMapper.selectUserList(sysUser);
		if (userList.size() == 0) {
			throw new CustomException("用户信息:" + emTask.getExecuter() + "在系统中不存在，请核实");
		}
		emTask.setExecuter(userList.get(0).getUserName());

		// 校验设备在系统中是否存在
		EmEquip queryEmEquip = new EmEquip();
		queryEmEquip.setId(Long.valueOf(emTask.getEqKey()));
		List<EmEquip> equipList = emEquipMapper.selectEmEquipList(queryEmEquip);
		if (equipList.size() == 0) {
			throw new CustomException("设备条码:" + emTask.getEqKey() + "在系统中不存在，请核实");
		}

		// 校验部门信息在系统是否存在
		SysDept dept = new SysDept();
		dept.setParentId(Long.valueOf(emTask.getSiteKey()));
		dept.setDeptName(emTask.getDepartment());
		List<SysDept> deptList = deptMapper.selectDeptList(dept);
		if (deptList.size() == 0) {
			throw new CustomException("部门:" + emTask.getDepartment() + "在系统中不存在，请核实");
		}

		emTask.setEqKey(String.valueOf(equipList.get(0).getId()));
		emTask.setCreateTime(DateUtils.getNowDate());
		return emTaskMapper.insertEmTask(emTask);
	}

	/**
	 * 修改任务管理
	 * 
	 * @param emTask 任务管理
	 * @return 结果
	 */
	@Override
	public int updateEmTask(EmTask emTask,String username) {


		emTask.setModifiedBy(username);
		emTask.setModificationTime(new Date());

		// 校验设备在系统中是否存在
		EmEquip queryEmEquip = new EmEquip();
		queryEmEquip.setName(emTask.getEqKey());
		List<EmEquip> equipList = emEquipMapper.selectEmEquipList(queryEmEquip);
		if (equipList.size() == 0) {
			throw new CustomException("设备条码:" + emTask.getEqKey() + "在系统中不存在，请核实");
		}

		SysUser sysUser = new SysUser();
		sysUser.setNickName(emTask.getExecuter());
		List<SysUser> userList = sysUserMapper.selectUserList(sysUser);
		if (userList.size() == 0) {
			throw new CustomException("用户信息:" + emTask.getExecuter() + "在系统中不存在，请核实");
		}
		emTask.setExecuter(userList.get(0).getUserName());
		
		// 校验部门信息在系统是否存在
		SysDept dept = new SysDept();
		dept.setParentId(Long.valueOf(emTask.getSiteKey()));
		dept.setDeptName(emTask.getDepartment());
		List<SysDept> deptList = deptMapper.selectDeptList(dept);
		if (deptList.size() == 0) {
			throw new CustomException("部门:" + emTask.getDepartment() + "在系统中不存在，请核实");
		}
		emTask.setEqKey(String.valueOf(equipList.get(0).getId()));
		return emTaskMapper.updateEmTask(emTask);
	}

	/**
	 * 批量删除任务管理
	 * 
	 * @param taskCodes 需要删除的任务管理ID
	 * @return 结果
	 */
	@Override
	public int deleteEmTaskByIds(String[] taskCodes,String username) {
		for (String taskCode : taskCodes) {
			EmTask dictType = selectEmTaskById(taskCode);
			EmTaskItem emTaskItem = new EmTaskItem();
			emTaskItem.setTaskKey(dictType.getTaskCode());
			List<EmTaskItem> items = emTaskItemMapper.selectEmTaskItemList(emTaskItem);
			if (items.size() > 0) {
				Long[] ids = new Long[items.size()];
				for (int i = 0; i < items.size(); i++) {
					ids[i] = items.get(i).getId();
				}
				emTaskItemMapper.deleteEmTaskItemByIds(ids);
			}
//			if (emTaskItemMapper.countItemByCode(dictType.getTaskCode()) > 0) {
//				throw new CustomException(String.format("%1$s已存在任务项不能删除", dictType.getTaskCode()));
//			}
		}
		return emTaskMapper.deleteEmTaskByIds(taskCodes,username,new Date());
	}

	/**
	 * 删除任务管理信息
	 * 
	 * @param taskCode 任务管理ID
	 * @return 结果
	 */
	@Override
	public int deleteEmTaskById(String taskCode,String username) {
		return emTaskMapper.deleteEmTaskById(taskCode,username,new Date());
	}

	@Override
	public List<EmTask> selectRandomEmTaskListQC(EmTask task) {

		List<EmTask> emTasks = emTaskMapper.selectRandomEmTaskListQC(task);

		//三片(需求变更，删掉)
//		if("270".equals(task.getHangameKey())){
//			emTasks = emTasks.stream().filter(task2 -> {
//				String taskCode = task2.getTaskCode();
//				EmTaskItem taskItem = new EmTaskItem();
//				taskItem.setTaskKey(taskCode);
//				List<EmTaskItem> emTaskItems = this.emTaskItemMapper.selectEmTaskItemList(taskItem);
//
//				int i = 0;
//				for (EmTaskItem item : emTaskItems) {
//					if ("4".equals(item.getExecuteMeans())) {
//						i++;
//					}
//				}
//				return  i != emTaskItems.size();
//			}).collect(Collectors.toList());
//		}

		return emTasks;
	}

	@Override
	public List<EmTask> selectNoFinishEmTaskList(EmTask emTask) {
		return emTaskMapper.selectNoFinishEmTaskList(emTask);
	}

	@Override
	public List<EmTask> selectRandomEmTaskList(EmTask emTask) {
		return emTaskMapper.selectRandomEmTaskList(emTask);
	}
}
